﻿using IBll;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model;
using NPOI.XSSF.UserModel;
using Microsoft.AspNetCore.StaticFiles;
namespace WebApl.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class SosConeController : ControllerBase, ISosConebLL
    {

        /// <summary>
        /// 实现方法
        /// </summary>
        /// <param name="bll"></param>
         public SosConeController(ISosConebLL bll,ILogger<SosConeController> logger)
        {
            Bll = bll;
            Logger = logger;
        }

        public ISosConebLL Bll { get; }
        public ILogger<SosConeController> Logger { get; }

        /// <summary>
        /// 新增  康复理疗
        /// </summary>
        /// <param name="t">数据信息</param>
        /// <returns>返回值受影响行数</returns>
        /// 
        [HttpPost]
        public Task<int> AddAnync(SosCone sosCone)
        {
            try//异常处理
            {
                Logger.LogInformation($"{sosCone}添加一条信息");

                 return Bll.AddAnync(sosCone);
            }
            catch (Exception)
            {

                throw;
            }
        }



        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns>通过id查询一条数据</returns>
        /// 
        [HttpGet]
        public Task<int> DeleteState(int id)
        {
            try//异常处理
            {
                 return Bll.DeleteState(id);
            }
            catch (Exception ex)
            {
                Logger.LogError(ex.Message);
                throw;
            }
        }



        /// <summary>
        /// 批量修改
        /// </summary>
        /// <param name="idsstaings">主键id</param>
        /// <returns>通过id查询一条数据</returns>
        /// 

        [HttpDelete] 
        public Task<int> Pstate(string idsstaings)
        {
            try//异常处理
            {
                 return Bll.Pstate  (idsstaings);
            }
            catch (Exception ex)
            {

                Logger.LogError(ex.Message);
                throw;
            }
        }



        /// <summary>
        /// 显示查询分页
        /// </summary>
        /// <param name="name">项目名称</param>
        /// <param name="index">页数</param>
        /// <param name="size">条数</param>
        /// <returns>通过查询姓名名称获取一条 数据</returns>
        /// 

        [HttpGet]
        public Dto ShowSosCone(string? name, int index, int size)
        {
            try//异常处理
            {
                 return Bll.ShowSosCone (name, index, size);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 导出
        /// </summary>
        /// <returns>返回受影响行数</returns>
        /// 
        [HttpPost]
        public List<SosCone> GetSosCones()
        {
            try//异常处理
            {
                 return Bll.GetSosCones();



            }
            catch (Exception)
            {

                throw;
            }

        }


        /// <summary>
        /// 导出
        /// </summary>
        /// <returns></returns>
        /// 

        [HttpPost]
         public IActionResult ExEcl()
        {
            try//异常处理
            {

                List<SosCone> shww=Bll.GetSosCones ();

                 XSSFWorkbook sheets = new XSSFWorkbook ();
                var sheet = sheets.CreateSheet("康复理疗信息");
                  var row= sheet.CreateRow(0);
                row.CreateCell(0).SetCellValue("id");
                row.CreateCell(1).SetCellValue("项目名称");
                row.CreateCell(2).SetCellValue("项目编号");
                row.CreateCell(3).SetCellValue("服务时长");
                row.CreateCell(4).SetCellValue("服务人数");
                row.CreateCell(5).SetCellValue("佣金");
                row.CreateCell(6).SetCellValue("项目说明");
                row.CreateCell(7).SetCellValue("分类名称");
                row.CreateCell(8).SetCellValue("状态");
                row.CreateCell(9).SetCellValue("删除");

                int num = 1;

                foreach (SosCone s in shww)
                {
                    var row1 = sheet.CreateRow(num);
                    row1.CreateCell(0).SetCellValue(s.SosConeId);
                    row1.CreateCell(1).SetCellValue(s.SosConeNmae);
                    row1.CreateCell(2).SetCellValue(s.Code);
                    row1.CreateCell(3).SetCellValue(s.SosConeSC);
                    row1.CreateCell(4).SetCellValue(s.SosConeNum);
                    row1.CreateCell(5).SetCellValue((double)s.Price);
                    row1.CreateCell(6).SetCellValue(s.SosConeType);
                    row1.CreateCell(7).SetCellValue(s.SosConeTypeId);
                    row1.CreateCell(8).SetCellValue(s.State==true?"启用":"禁用");
                    row1.CreateCell(9).SetCellValue(s.isDelete==true?"已删除":"未删除");

                     num++;
                }

                 MemoryStream memory = new MemoryStream();
                sheets.Write(memory);
                byte[] data = memory.ToArray();

                var mimn = new Microsoft.AspNetCore.StaticFiles.FileExtensionContentTypeProvider().Mappings[".xlsx"];
                return File(data, mimn, ("康复理疗信息.xlsx"));





            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
